package com.study.cloud.controller;

import com.study.cloud.MemberDTO;
import com.study.cloud.config.FilterExcludeUrl;
import com.study.cloud.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Member;

@RestController
@RequestMapping("/member")
public class MemberController {

    final static Logger logger = LoggerFactory.getLogger(MemberController.class);


    /**
     * 1. get请求,参数为字段类型;
     * 2. get请求,参数是实体对象类型;
     * 3. get请求,没有参数
     *
     * 11. post请求,参数是实体对象json类型
     * 12. post请求,没有请求参数
     * 13. post请求,参数是表单类型提交
     * @param name
     * @return
     */

    @Value("${excludea.url}")
    private String exclude;

    @Autowired
    FilterExcludeUrl url;

    @GetMapping("/hello")
    public MemberDTO hello(String name){
        System.out.println(url.getExclude());
        MemberDTO userDTO = new MemberDTO();
        userDTO.setId(10);
        userDTO.setName(name);
        if(1==1){
//            throw new RuntimeException("出现错误了");
        }
        logger.info(" AA 进入member 服务中");
        return userDTO;
    }

    @GetMapping("/hello1")
    public String hello1(MemberDTO memberDTO){
        return "你好: "+memberDTO.getName();
    }


    @PostMapping("/hello3")
    public MemberDTO hello3(@RequestBody MemberDTO memberDTO){
        memberDTO.setName(memberDTO.getName()+"==change");
        return memberDTO;
    }


    @PostMapping("/hello4")
    public MemberDTO hello4(String name){
        MemberDTO dto = new MemberDTO();
        dto.setId(1000);
        dto.setName(name);
        return dto;
    }


    @GetMapping("/info")
    public MemberDTO getUserInfo(Integer id) {

        if(id != null && id %2 ==0){
            try {
                Thread.sleep(id*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else if(id != null && id >2){
            try {
                Thread.sleep(id*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        MemberDTO userDTO = new MemberDTO();
        userDTO.setId(10);
        userDTO.setName("zhangsan");
        logger.info("进入member 服务中");
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(UserDTO.class);
        rootBeanDefinition.setAttribute("",null);

        BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(UserDTO.class);
        definitionBuilder.addPropertyValue("",null);
        BeanDefinition rawBeanDefinition = definitionBuilder.getRawBeanDefinition();


        return userDTO;
    }


}
